﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Isha.Entities;
using Isha.Infrastructure;
using Isha.Services;
using System.Web.Security;
using System.Xml;
using Isha.Extensions;

namespace Isha.Web.Mvc
{
    public abstract class IshaController : Controller
    {
        public IshaController()
        {

        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IContentItemService contentItemService = IshaContext.Current.ServiceContext.ContentItemService;
            ContentItem contentItem = IshaContext.Current.ContentItem;
            IshaModel model = IshaContext.Current.Model;
            IContentCommand command = IshaContext.Current.ContentCommand;

            model.Add("Navigation", IshaContext.Current.Navigation);
            model.Add("Breadcrumbs", IshaContext.Current.BreadcrumbItems);
            model.Add("ContentItem", contentItem);            
 
            if (contentItem.Title != null)
            {
                model.Add(IshaContext.Current.ContentItem.Type.Name, contentItem.Content);

                if (IshaContext.Current.ContentItem.ContentItemID == IshaContext.Current.Configuration.Home)
                    IshaContext.Current.ContentItem.IsHome = true;

                contentItem.ContentItems.Clear();
                contentItem.ContentItems = contentItemService.FetchContentItemsByParent(contentItem.ContentItemID).ToList();

                if (IshaContext.Current.Command == Commands.Detail || IshaContext.Current.Command == Commands.Create || IshaContext.Current.Command == Commands.Edit)
                    command.Execute();


            }

            base.OnActionExecuting(filterContext);
        }

        public virtual ActionResult Create()
        {
            return base.View("../portal_factory/create");
        }

        [ValidateInput(false)]
        [HttpPost]
        public virtual ActionResult Create(FormCollection collection)
        {
            string title = Request["ContentItem_Title"];
            string description = Request["ContentItem_Description"];

            IshaContext.Current.Request.FormCollection = collection;

            IContentItemService contentItemService = IshaContext.Current.ServiceContext.ContentItemService;
            IContentTypeService contentTypeService = IshaContext.Current.ServiceContext.ContentTypeService;

            ContentItem contentItem = IshaContext.Current.ContentItem;
            IContentCommand contentCommand = IshaContext.Current.ContentCommand;

            contentItem.Title = title;
            contentItem.Description = description;
            contentItem.ParentItemID = IshaContext.Current.BreadcrumbItems.Last().ContentItemID;
            contentItem.AllowComments = false;
            contentItem.CreatedBy = Membership.GetUser().UserName;
            contentItem.CreatedOn = DateTime.Now;
            contentItem.DisplayOrder = 12;
            contentItem.ContentTypeID = contentTypeService.GetContentType(IshaContext.Current.ContentItem.Type.Name).ContentTypeID;

            IshaContext.Current.ContentItem = contentItem;

            contentCommand.Execute();

            contentItemService.CreateContentItem(IshaContext.Current.ContentItem);

            if (contentItem.ParentItemID == 1)
                IshaContext.Current.Navigation = contentItemService.FetchContentItemsByParent(1);

            Response.RedirectPermanent(Request.RawUrl
                .ToString().ToLower().Replace("portal_factory/" + IshaContext.Current.ContentItem.Type.Name.ToLower() + "/" + IshaContext.Current.Command.ToString().ToLower(), "") + contentItem.Title);

            return RedirectToAction("Index");
        }

        public virtual ActionResult Edit()
        {
            return base.View("../portal_factory/edit");
        }

        [ValidateInput(false)]
        [HttpPost]
        public virtual ActionResult Edit(FormCollection collection)
        {
            string title = collection["ContentItem_Title"];
            string description = collection["ContentItem_Description"];

            IshaContext.Current.Request.FormCollection = collection;

            IContentItemService contentItemService = IshaContext.Current.ServiceContext.ContentItemService;
            ContentItem contentItem = IshaContext.Current.ContentItem;
            IContentTypeService contentTypeService = IshaContext.Current.ServiceContext.ContentTypeService;
            IContentCommand contentCommand = IshaContext.Current.ContentCommand;

            contentItem.Title = title;
            contentItem.Description = description;
            contentItem.AllowComments = false;
            contentItem.ModifiedBy = Membership.GetUser().UserName;
            contentItem.ModifiedOn = DateTime.Now;

            IshaContext.Current.ContentItem = contentItem;

            contentCommand.Execute();

            contentItemService.UpdateContentItem(IshaContext.Current.ContentItem);

            if (contentItem.ParentItemID == 1)
                IshaContext.Current.Navigation = contentItemService.FetchContentItemsByParent(1);

            Response.RedirectPermanent(Request.RawUrl.ToString().ToLower().Replace("/portal_factory/edit", ""));

            return RedirectToAction("Index");
        }

        #region View

        protected new ViewResult View()
        {
            return base.View(IshaContext.Current.Command.ToString());
        }

        protected new ViewResult View(object model)
        {
            return base.View(IshaContext.Current.Command.ToString().ToLower(), model);
        }

        protected new ViewResult View(IView view)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        protected new ViewResult View(string viewName)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        protected new ViewResult View(IView view, object model)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        protected new ViewResult View(string viewName, object model)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        protected new ViewResult View(string viewName, string masterName)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        protected new ViewResult View(string viewName, string masterName, object model)
        {
            throw new InvalidOperationException("You can't use this method with Isha.");
        }

        #endregion

        public virtual ActionResult ListContents()
        {
            return base.View("../portal_factory/folder_contents");
        }

        public virtual ActionResult Index()
        {
            return View();
        }
    }
}
